/**
 * Created by Been on 2017/5/17.
 */
var VM = new Vue({
    el:"#deptApp",
    created:function(){
        this.lay();
    },
    data:{
        totalNum:"",
        deptLists:[],
        delMultiFlag:[],
        allChoseFlag:false,
        keyWord:''
    },
    methods:{
        lay:function(){
            var _this = this;
            layui.use(['laydate','element','laypage','layer','form'], function(){

               var  laypage = layui.laypage,//分页
                layer = layui.layer;//弹出层

                //以上模块根据需要引入
                getPageData(1);
                /*请求后台数据，分页请求*/
                function getPageData(curr) {

                    $.ajax({
                        url:"../../department/list.do?pagenum="+curr+"&pagesize="+5+"&deptName="+_this.keyWord,
                        dataType:"json",
                        type:"POST",
                        async:false,
                        success:function (res) {
                            if(res.msg=="success"){
                                var list = res.result.result;
                                _this.deptLists = list;
                                console.log(list);
                                laypage.pages = res.result.total;
                                _this.totalNum = res.result.total;
                                //当只有一页时
                                if(res.result.total > 5){
                                    $(".pageShowFlag").css({"display":"none"});
                                    $(".pageHideFlag").css({"display":"block"});
                                }else if(res.result.total > 0 ){
                                    $(".pageShowFlag").css({"display":"block"});
                                    $(".pageHideFlag").css({"display":"none"});
                                }else{
                                    $(".pageShowFlag").css({"display":"none"});
                                    $(".pageHideFlag").css({"display":"none"});
                                    layer.msg("没有查询到数据！", {icon: 1});
                                }


                                laypage({
                                    cont: 'pagination', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="pagination"></div>
                                    pages: Math.ceil(res.result.total/5), //通过后台拿到的总页数
                                    curr: curr || 1, //当前页
                                    jump: function(obj, first){ //触发分页后的回调
                                        if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                                            getPageData(obj.curr);

                                        }
                                    }
                                });
                            }else{
                                layer.msg("没有查询到数据！", {icon: 1})
                            }
                        },
                        error:function (XMLHttpRequest) {
                            layer.msg("系统错误", {icon: 1});
                        }
                    })
                }
            });
        },
        allChose:function(){
            var _this = this;
            this.allChoseFlag = !this.allChoseFlag;
            this.deptLists.forEach(function(item,index){
                if(typeof item.checked == "undefined"){
                    Vue.set(item,"checked",true);
                    _this.delMultiFlag.push(item.id);
                }else{
                    item.checked = _this.allChoseFlag;
                    _this.delMultiFlag = [];
                }
            });
        },
        selectSelf:function(item){
            if(typeof item.checked == "undefined"){
                Vue.set(item,"checked",true);
            }else{
                item.checked = !item.checked;
            }
        },
        editDept:function(item){
            admin_edit("编辑","../../department/info.do?id="+ item,item,'800','600')
        },
        addDept:function(item){
            admin_add("新增","../../department/info.do",'800','600')
        },
        delDept:function(item){
            admin_del("删除","../../department/delete.do?id="+ item,'800','600')
        },
        delMulti:function(){
            var _this = this;
            this.notiveLists.forEach(function (item,index) {
                _this.delMultiFlag.forEach(function (value,i) {
                    if(item.id == value){
                        item.id = 0;
                    }
                });
            });
        },
        deptShow:function (item,index) {
            var _this = this;
            console.log(item.id);
            this.deptLists.forEach(function(dept,i){

                if(dept.parentDeptId == item.id){
                    var pid = dept.parentDeptId;
                    // console.log(dept.parentDeptId);

                    if(typeof dept.showSubDept == "undefined"){
                        Vue.set(dept,"showSubDept",true);
                    }else{
                        dept.showSubDept = !dept.showSubDept;
                    }
                }
            });
        }
    }
});//vue 实力结束
